package com.example.yunJie.digitalSentry.control;


import com.example.yunJie.digitalSentry.entity.MarketEntity;
import com.example.yunJie.digitalSentry.entity.MarketPostEntity;
import com.example.yunJie.digitalSentry.entity.PostEntity;
import com.example.yunJie.digitalSentry.service.MarketPostService;
import com.example.yunJie.digitalSentry.service.MarketService;
import com.example.yunJie.digitalSentry.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Post")
public class PostControl {
    @Autowired
    private PostService postService;
    @Autowired
    private MarketPostService marketPostService;
    @Autowired
    private MarketService marketService;


    @RequestMapping("/add")
    public void insert(PostEntity postEntity) {
        postService.insert(postEntity);
        PostEntity idByPostName = postService.findIdByPostName(postEntity.getPostName());
        MarketEntity idByMarketName = marketService.findIdByMarketName(postEntity.getMarketName());
        MarketPostEntity marketPostEntity = new MarketPostEntity();
        marketPostEntity.setPostId(String.valueOf(idByPostName.getId()));
        marketPostEntity.setMarketId(String.valueOf(idByMarketName.getId()));
        marketPostService.insert(marketPostEntity);

    }

    @RequestMapping("/update")
    public int update(PostEntity postEntity) {
        postService.findId(String.valueOf(postEntity.getId()));
        postService.update(postEntity);
        PostEntity idByPostName = postService.findIdByPostName(postEntity.getPostName());
        MarketEntity idByMarketName = marketService.findIdByMarketName(postEntity.getMarketName());
        MarketPostEntity marketPostEntity = marketPostService.find(String.valueOf(idByMarketName.getId()), String.valueOf(idByPostName.getId()));
        return marketPostService.update(marketPostEntity);
    }

    @RequestMapping("/deleteById")
    public int deleteById(int id) {
        return postService.deleteById(id);
    }

    @RequestMapping("/deleteIdList")
    public int deleteIdList(List<Integer> id) {
        return postService.deleteIdList(id);
    }

    @RequestMapping("/findId")
    public PostEntity findId(String id) {
        return postService.findId(id);
    }

    @RequestMapping("/findAll")
    public List<PostEntity> findAll() {
        return postService.findAll();
    }

    //根据市场名称查找岗位名称
    @RequestMapping("/findByMarketName")
    public List<PostEntity> findByMarketName(String marketName) {
        return postService.findByMarketName(marketName);
    }

}
